﻿using Newtonsoft.Json.Linq;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Pay.Token.Controllers {
    /// <summary>
    /// 公告
    /// </summary>
    [RoutePrefix("notice")]
    public class NoticeController : BaseController {
        private INoticeService _iNoticeService;
        private IMessagePushService _iMessagePushService;
        public NoticeController(INoticeService iNoticeService, IMessagePushService iMessagePushService) {
            _iNoticeService = iNoticeService;
            _iMessagePushService = iMessagePushService;
        }

        /// <summary>
        /// 获得首页最新公告
        /// </summary>
        /// <returns></returns>
        [Route("get_index_notice"), HttpGet]
        public ResultMessage<notice_dto> GetIndexNotice() {
            var result = _iNoticeService.GetAppIndexNotice(UserId);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获得通知公告分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_notices"), HttpGet]
        public ResultMessage<PagedResult<notice_dto>> GetNoticePageList([FromUri]NoticeApiQuery query) {
            if (query.type != 1 && query.type != 3)
                query.type = 1;
            var list = _iNoticeService.GetAppNoticePageList(query, UserId);
            var result = new PagedResult<notice_dto>(list.rows, list.total_result, list.page, list.size);
            return ResultToJson.Json(result);
        }

        /// <summary>
        /// 获得公告详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("get_notice_detail"), HttpGet]
        public ResultMessage<notice_dto> GetNotice([FromUri]notice_query model) {
            var result = _iNoticeService.AppGetNotice(model.id, UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 用户消息列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("get_user_notices"), HttpGet]
        public ResultMessage<PagedResult<message_push_dto>> GetUserNoticePageList([FromUri]MessageQuery query) {
            query.user_id = UserId;
            var list = _iMessagePushService.Get(query);
            return ResultToJson.Json(list);
        }
        /// <summary>
        /// 用户消息详情
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("get_user_notice_detail"), HttpGet]
        public ResultMessage<message_push_dto> GetUserNotice([FromUri]notice_query model) {
            var result = _iMessagePushService.Get(model.id);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获取公告消息未读数量等
        /// </summary>
        /// <returns></returns>
        [Route("get_notice_message"), HttpGet]
        public ResultMessage<notice_message_dto> GetNoticeMessage() {
            var result = _iNoticeService.GetNoticMessage(UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获取消息未读数量
        /// </summary>
        /// <returns></returns>
        [Route("unreads"), HttpGet]
        public ResultMessage<int> GetUnReads() {
            var result = _iNoticeService.GetUnReads(UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 获取消息列表四大块
        /// </summary>
        /// <returns></returns>
        [Route("message_unreads"), HttpGet]
        public ResultMessage<List<message_item_ex>> GetNoticeMessageEx() {
            var result = _iNoticeService.GetNoticMessageEx(UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 删除公告
        /// </summary>
        /// <returns></returns>
        [Route("delete_notice"), HttpPost]
        public ResultMessage<bool> DeleteNotice([FromBody]JObject id) {
            var value = GetJsonValue<long>("id", id);
            var result = _iNoticeService.APPDeleteNotice(value,UserId);
            return ResultToJson.Json(result > 0);
        }
        /// <summary>
        /// 删除消息
        /// </summary>
        /// <returns></returns>
        [Route("delete_message"), HttpPost]
        public ResultMessage<bool> DeleteMessage([FromBody]JObject id) {
            var value = GetJsonValue<long>("id", id);
            var result = _iMessagePushService.Delete(new List<long> { value });
            return ResultToJson.Json(result > 0);
        }
    }
}
